package org.finesys.gateway.factory;

import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.web.server.ServerWebExchange;

import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * 自定义断言类，控制时间用。区间内可用
 * -GatewayTimeBetween=6:00,23:00
 */
public class GatewayTimeBetweenRoutePredicateFactory extends AbstractRoutePredicateFactory<GatewayTimeBetweenConfig> {


    public GatewayTimeBetweenRoutePredicateFactory() {
        super(GatewayTimeBetweenConfig.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList(GatewayTimeBetweenConfig.Fields.startTime, GatewayTimeBetweenConfig.Fields.endTime);
    }

    @Override
    public Predicate<ServerWebExchange> apply(GatewayTimeBetweenConfig config) {
        return (serverWebExchange -> LocalTime.now().isAfter(config.getStartTime()) && LocalTime.now().isBefore(config.getEndTime()));
    }
}
